﻿using CPPEI.Coolzon.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.Designer.Views
{
    /// <summary>
    /// AddtitionPropertyView.xaml 的交互逻辑
    /// </summary>
    public partial class AddtitionPropertyWindow : Window
    {
        private Part _selectedPart;
        private bool _isEditable;
        public AddtitionPropertyWindow(Part selectedPart, string partType, bool isEditable)
        {
            InitializeComponent();
            _selectedPart = selectedPart;
            _type.Text = partType.TrimStart('(').TrimEnd(')');
            _isEditable = isEditable;
            Init();
        }

        private void Init()
        {
            _grid.DataContext = _selectedPart.XGetMetadata();
            if (_selectedPart is ScenePart)
            {
                InitScenePart();
            }
            else if(_selectedPart is AreaPart)
            {

            }
            else if(_selectedPart is Part)
            {
                InitPart();
            }
            _caption.IsReadOnly = !_isEditable;
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.DataBind, new Action(() =>
            {
                _caption.TextChanged += _caption_TextChanged;
            }));
        }

        private void InitPart()
        {
            _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(0, GridUnitType.Auto) });
            TextBlock tb = new TextBlock();
            tb.Margin = new Thickness(3, 5, 3, 3);
            tb.Text = "是否预加载";
            tb.ToolTip = "目前只适用于视频组件";
            CheckBox cb = new CheckBox();
            cb.Margin = new Thickness(3);
            cb.IsEnabled = _isEditable;
            Binding binding = new Binding();
            binding.Source = _selectedPart.XGetMetadata();
            binding.Path = new PropertyPath("IsPreload");
            binding.Mode = BindingMode.TwoWay;
            cb.SetBinding(CheckBox.IsCheckedProperty, binding);

            Grid.SetColumn(tb, 0);
            Grid.SetColumn(cb, 1);
            Grid.SetRow(tb, 3);
            Grid.SetRow(cb, 3);
            _grid.Children.Add(tb);
            _grid.Children.Add(cb);
            cb.Checked += Cb_Checked;
            cb.Unchecked += Cb_Unchecked;
        }

        private void InitScenePart()
        {
            _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(0, GridUnitType.Auto) });
            TextBlock tb = new TextBlock();
            tb.Margin = new Thickness(3, 5, 3, 3);
            tb.Text = "是否显示在导航上";
            CheckBox cb = new CheckBox();
            cb.IsEnabled = _isEditable;
            cb.Margin = new Thickness(3);
            Binding binding = new Binding();
            binding.Source = _selectedPart.XGetMetadata();
            binding.Path = new PropertyPath("IsShowInNavigation");
            binding.Mode = BindingMode.TwoWay;
            cb.SetBinding(CheckBox.IsCheckedProperty, binding);

            Grid.SetColumn(tb, 0);
            Grid.SetColumn(cb, 1);
            Grid.SetRow(tb, 3);
            Grid.SetRow(cb, 3);
            _grid.Children.Add(tb);
            _grid.Children.Add(cb);
            cb.Checked += Cb_Checked;
            cb.Unchecked += Cb_Unchecked;

            _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(0, GridUnitType.Auto) });
            tb = new TextBlock();
            tb.Margin = new Thickness(3, 5, 3, 3);
            tb.Text = "是否水平循环显示";
            cb = new CheckBox();
            cb.IsEnabled = _isEditable;
            cb.Margin = new Thickness(3);
            binding = new Binding();
            binding.Source = _selectedPart.XGetMetadata();
            binding.Path = new PropertyPath("IsHorizontalLoopRender");
            binding.Mode = BindingMode.TwoWay;
            cb.SetBinding(CheckBox.IsCheckedProperty, binding);

            Grid.SetColumn(tb, 0);
            Grid.SetColumn(cb, 1);
            Grid.SetRow(tb, 4);
            Grid.SetRow(cb, 4);
            _grid.Children.Add(tb);
            _grid.Children.Add(cb);
            cb.Checked += Cb_Checked;
            cb.Unchecked += Cb_Unchecked;

            _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(0, GridUnitType.Auto) });
            tb = new TextBlock();
            tb.Margin = new Thickness(3, 5, 3, 3);
            tb.Text = "是否允许交互场景中的区域";
            cb = new CheckBox();
            cb.IsEnabled = _isEditable;
            cb.Margin = new Thickness(3);
            binding = new Binding();
            binding.Source = _selectedPart.XGetMetadata();
            binding.Path = new PropertyPath("IsAllowSwapArea");
            binding.Mode = BindingMode.TwoWay;
            cb.SetBinding(CheckBox.IsCheckedProperty, binding);

            Grid.SetColumn(tb, 0);
            Grid.SetColumn(cb, 1);
            Grid.SetRow(tb, 5);
            Grid.SetRow(cb, 5);
            _grid.Children.Add(tb);
            _grid.Children.Add(cb);
            cb.Checked += Cb_Checked;
            cb.Unchecked += Cb_Unchecked;
        }


        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void _caption_TextChanged(object sender, TextChangedEventArgs e)
        {
            UIContext.Current.CurrentEditedNode.Header = _caption.Text;
            UIContext.Current.CurrentEditedNode.Isdirty = true;
        }
        private void Cb_Checked(object sender, RoutedEventArgs e)
        {
            UIContext.Current.CurrentEditedNode.Isdirty = true;
        }
        private void Cb_Unchecked(object sender, RoutedEventArgs e)
        {
            UIContext.Current.CurrentEditedNode.Isdirty = true;
        }
    }
}
